package com.ziyun.erp.web.api.goods;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.utility.ArrayUtil;
import com.utility.JsonUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ziyun.erp.modules.freightages.entity.FreightAreaEntity;
import com.ziyun.erp.modules.freightages.entity.FreightTemplateEntity;
import com.ziyun.erp.modules.freightages.entity.SdbEctoolsRegionsEntity;
import com.ziyun.erp.modules.freightages.service.FreightAreaService;
import com.ziyun.erp.modules.freightages.service.FreightTemplateService;
import com.ziyun.erp.modules.freightages.service.SdbEctoolsRegionsService;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;

/**
 * 运费模板表
 *
 * @author
 * @email
 * @date 2018-08-10 15:38:32
 */
@RestController
@RequestMapping("port/freightages/freighttemplate")
public class FreightTemplateControllerApi {
	@Autowired
	private FreightTemplateService freightTemplateService;

	@Autowired
	private FreightAreaService freightAreaService;

	@Autowired
	private SdbEctoolsRegionsService sdbEctoolsRegionsService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
//	@RequiresPermissions("freightages:freighttemplate:list")
	public R list(@RequestBody Map<String, Object> params) {
//		PageUtils page = freightTemplateService.queryPage(params); jj
		Map params1 = (Map) params.get("params");
		Map params2 = (Map) params1.get("params");
		return sdbEctoolsRegionsService.getUserData(params2);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info")
	// @RequiresPermissions("freightages:freighttemplate:info")
	public R info(@RequestBody Map paramBodyMap) {
		Map params = (Map) paramBodyMap.get("params");
		Integer templateId = Integer.parseInt(params.get("templateId").toString());
		FreightTemplateEntity freightTemplate = freightTemplateService.selectById(templateId);
		HashMap<String, Object> map = new HashMap<>();
		map.put("template_id", templateId);
		map.put("disabled", 0);
		List<FreightAreaEntity> freightAreaList = freightAreaService.selectByMap(map);
		return R.ok().put("freightTemplate", freightTemplate).put("freightAreaList", freightAreaList);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
//	@RequiresPermissions("freightages:freighttemplate:save")
	public R save(@RequestBody Map paramBodyMap) {
		Map params = (Map) paramBodyMap.get("params");
		String freightTemplate = JsonUtils.object2String(params.get("freightTemplate"));
		FreightTemplateEntity freightTemplateEntity = JsonUtils.string2Object(freightTemplate, FreightTemplateEntity.class);
		// freightTemplateService.insert(freightTemplate);
		freightTemplateService.insertFreightTemplate(freightTemplateEntity);
		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
//	@RequiresPermissions("freightages:freighttemplate:update")
	public R update(@RequestBody Map paramBodyMap) {
		Map params = (Map) paramBodyMap.get("params");
		String freightTemplate = JsonUtils.object2String(params.get("freightTemplate"));
		FreightTemplateEntity freightTemplateEntity = JsonUtils.string2Object(freightTemplate, FreightTemplateEntity.class);
		freightTemplateService.updateFreightTemplate(freightTemplateEntity);
		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
//	@RequiresPermissions("freightages:freighttemplate:delete")
	public R delete(@RequestBody Map paramBodyMap) {
		//@RequestBody Integer[] templateIds
		Map params = (Map) paramBodyMap.get("params");
		List templateIds = (List) params.get("templateIds");
		Integer[] ids = new Integer[templateIds.size()];
		for(int i=0;i<templateIds.size();i++){
			ids[i] = Integer.parseInt(templateIds.get(i).toString());
		}
		freightTemplateService.flagDelete(ids);
		return R.ok();
	}

	/**
	 * 保存
	 */
	@RequestMapping("/allList")
	public R allList(@RequestBody Map paramBodyMap) {
		Map params = (Map) paramBodyMap.get("params");
		List<FreightTemplateEntity> freightTemplateList = freightTemplateService.selectByMap(params);
		return R.ok().put("freightTemplateList", freightTemplateList);
	}

	@RequestMapping("/newList")
	public R newList(@RequestBody Map<String, Object> paramBodyMap){
		Map params = (Map) paramBodyMap.get("params");
		return freightTemplateService.newList(params);
//        return R.ok().put("page", page).put("allRegions",allRegions);
	}
}
